/* 
	The following code creates the dataset from the individual SOEP data
	files and replicates all Tables and Figures in "Rents, Refugees, and
	the Populist Radical Right" by Alexander Held and Pauliina Patana.
	
	Access to the data that is needed to replicate the findings from the
	study (SOEP-Core, 1984-2019, version 36, International Edition,
	https://doi.org/10.5684/soep.core.v36i) requires signing an agreement
	with DIW Berlin. For details, please see
	https://www.diw.de/en/diw_01.c.601584.en/data_access.html.
	
	For information on the German Socio-Economic Panel (SOEP), please see:
	Goebel, Jan, Markus M. Grabka, Stefan Liebig, Martin Kroh, David Richter,
	Carsten Schroeder, and Juergen Schupp. 2019. “The German Socio-Economic
	Panel.” Jahrbuecher fuer Nationaloekonomie und Statistik (Journal of
	Economics and Statistics) 239(2): 345-360.
	
	Structure of this do-file:
	- lines 30-1308: Code to create dataset
	- lines 1312-1667: Code to replicate Tables and Figures in the article
*/



* Before running the code below:
* Please place all the SOEP files in a folder on your computer and
* set the file paths below to this folder


**** A. MERGING SEPARATE SOEP DATASETS INTO ONE MASTER FILE ****

clear

global MY_PATH_IN   "/Users/[AUTHOR]/Desktop/Housing Project/SOEP/Stata/raw/"
global MY_PATH_OUT  "/Users/[AUTHOR]/Desktop/Housing Project/SOEP/Demographics/"
global MY_FILE_OUT  ${MY_PATH_OUT}new.dta
global MY_LOG_FILE  ${MY_PATH_OUT}new.log
capture log close
log using "${MY_LOG_FILE}", text replace
set more off

***PFAD***

use vhhnr shhnr unetto bfhhnr cnetto mhhnr pnetto fpop kpop dhhnr psample bhhhnr uhhnr nnetto bhnetto bbhhnr hhnr apop bhhnr khhnr binetto gnetto mpop enetto rpop lhhnr rhhnr bcnetto nhhnr vpop fnetto zpop jhhnr tpop ohhnr npop qpop banetto knetto anetto ipop opop bchhnr xpop jpop ghhnr cpop bpop bdhhnr bapop znetto onetto qnetto bepop bgpop tnetto lpop phhnr corigin bihhnr ynetto bcpop bgnetto bfnetto wpop bghhnr thhnr benetto bdnetto dpop ehhnr ypop wnetto behhnr bdpop bfpop hpop ppop yhhnr bbnetto ahhnr upop gebjahr bnetto dnetto lnetto epop qhhnr vnetto fhhnr chhnr snetto bhpop bahhnr whhnr sex xhhnr xnetto mnetto ihhnr inetto persnr jnetto spop gpop rnetto bbpop hhhnr bipop hnetto zhhnr /// 
using "${MY_PATH_IN}/ppfad.dta", clear

***NETTO VALUES - REFER TO SURVEY STATUS***

*This variable indicates available information and files for the entire SOEP individuals. Netto-codes 10-19 (and 29) define the respondents population of PGEN, the codes 20-28 indicate children, 30-39 unit-non-responses in partially realized households, and the codes 90-99 describe permanent (or temporary) dropouts. Further differentiations point to the survey instruments (questionnaires). The Codes 10-39 describe the population in realized (and partially realized households).*

keep if ( (bdnetto >= 10 | bdnetto < 20) | (bcnetto >= 10 | bcnetto < 20) | (onetto >= 10 | onetto < 20) | (cnetto >= 10 | cnetto < 20) | (binetto >= 10 | binetto < 20) | (unetto >= 10 | unetto < 20) | (pnetto >= 10 | pnetto < 20) | (hnetto >= 10 | hnetto < 20) | (wnetto >= 10 | wnetto < 20) | (jnetto >= 10 | jnetto < 20) | (knetto >= 10 | knetto < 20) | (bgnetto >= 10 | bgnetto < 20) | (fnetto >= 10 | fnetto < 20) | (banetto >= 10 | banetto < 20) | (enetto >= 10 | enetto < 20) | (gnetto >= 10 | gnetto < 20) | (benetto >= 10 | benetto < 20) | (qnetto >= 10 | qnetto < 20) | (dnetto >= 10 | dnetto < 20) | (bfnetto >= 10 | bfnetto < 20) | (mnetto >= 10 | mnetto < 20) | (bhnetto >= 10 | bhnetto < 20) | (bbnetto >= 10 | bbnetto < 20) | (vnetto >= 10 | vnetto < 20) | (ynetto >= 10 | ynetto < 20) | (rnetto >= 10 | rnetto < 20) | (znetto >= 10 | znetto < 20) | (lnetto >= 10 | lnetto < 20) | (nnetto >= 10 | nnetto < 20) | (bnetto >= 10 | bnetto < 20) | (snetto >= 10 | snetto < 20) | (tnetto >= 10 | tnetto < 20) | (anetto >= 10 | anetto < 20) | (xnetto >= 10 | xnetto < 20) | (inetto >= 10 | inetto < 20) )

***ONLY PRIVATE HOUSEHOLDS***

keep if ( (bdpop == 1 | bdpop == 2) | (bcpop == 1 | bcpop == 2) | (opop == 1 | opop == 2) | (cpop == 1 | cpop == 2) | (bipop == 1 | bipop == 2) | (upop == 1 | upop == 2) | (ppop == 1 | ppop == 2) | (hpop == 1 | hpop == 2) | (wpop == 1 | wpop == 2) | (jpop == 1 | jpop == 2) | (kpop == 1 | kpop == 2) | (bgpop == 1 | bgpop == 2) | (fpop == 1 | fpop == 2) | (bapop == 1 | bapop == 2) | (epop == 1 | epop == 2) | (gpop == 1 | gpop == 2) | (bepop == 1 | bepop == 2) | (qpop == 1 | qpop == 2) | (dpop == 1 | dpop == 2) | (bfpop == 1 | bfpop == 2) | (mpop == 1 | mpop == 2) | (bhpop == 1 | bhpop == 2) | (bbpop == 1 | bbpop == 2) | (vpop == 1 | vpop == 2) | (ypop == 1 | ypop == 2) | (rpop == 1 | rpop == 2) | (zpop == 1 | zpop == 2) | (lpop == 1 | lpop == 2) | (npop == 1 | npop == 2) | (bpop == 1 | bpop == 2) | (spop == 1 | spop == 2) | (tpop == 1 | tpop == 2) | (apop == 1 | apop == 2) | (xpop == 1 | xpop == 2) | (ipop == 1 | ipop == 2) )

***SAVE PFAD***

save "${MY_PATH_OUT}pfad.dta", replace 
clear

***PHRF - WEIGHTING FACTOR***

*standard individual weights*

use aphrf nphrf bhphrf wphrf rphrf qphrf tphrf bphrf bdphrf kphrf gphrf biphrf bbphrf uphrf bephrf zphrf mphrf hhnr fphrf lphrf ephrf xphrf prgroup bfphrf persnr yphrf sphrf jphrf iphrf ophrf vphrf bcphrf pphrf dphrf cphrf hphrf bgphrf baphrf biphrf /// 
using "${MY_PATH_IN}phrf.dta" 
save "${MY_PATH_OUT}hrf.dta", replace 
clear

***CREATE MASTER***

use "${MY_PATH_OUT}pfad.dta", clear
merge 1:1 persnr hhnr using "${MY_PATH_OUT}hrf.dta", keep(master match) nogen
save "${MY_PATH_OUT}master.dta", replace

*B. MERGE EQUIV FILES TOGETHER -- THESE ARE CROSS-NATIONAL EQUIVALENT FILES FOR EACH YEAR* 

use "${MY_PATH_IN}apequiv.dta"
merge 1:1 persnr using "${MY_PATH_IN}bpequiv.dta"
drop _merge

keep ahhnr bhhnr hid  pid persnr d11102ll e11106* d11101* d11106* e11101*  w11105* d11108* e11103* e11102* i11105* i11104* i11103* i11102* i11113* i11101* i11117* i11106* i11107* w11102* i11118* house* hsup* flossc* flossr* fhsup* fhouse* frenty* fjob1* e11201* i11205* i11218* i11209* renty* ijob1* ijob2* iself* kidy* i11110* x11103* x11105* w11101* w11108* lossc* lossr* d11104* d11109* e11105* w11109* e11104* d11112ll l11101* syear w11107*  

merge 1:1 persnr using "${MY_PATH_IN}cpequiv.dta"
drop _merge

keep ahhnr bhhnr chhnr hid  pid persnr d11102ll e11106* d11101* d11106* e11101*  w11105* d11108* e11103* e11102* i11105* i11104* i11103* i11102* i11113* i11101* i11117* i11106* i11107* w11102* i11118* house* hsup* flossc* flossr* fhsup* fhouse* frenty* fjob1* e11201* i11205* i11218* i11209* renty* ijob2* ijob1* iself* kidy* i11110* x11103* x11105* w11101* w11108* lossc* lossr* d11104* d11109* e11105* w11109* e11104* d11112ll l11101* syear   w11107*  

merge 1:1 persnr using "${MY_PATH_IN}dpequiv.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr hid  pid persnr d11102ll e11106* d11101* d11106* e11101*  w11105* d11108* e11103* e11102* i11105* i11104* i11103* i11102* i11113* i11101* i11117* i11106* i11107* w11102* i11118* house* hsup* flossc* flossr* fhsup* fhouse* frenty* fjob1* e11201* i11205* i11218* i11209* renty* ijob1* ijob2* iself* kidy* i11110* x11103* x11105* w11101* w11108* lossc* lossr* d11104* d11109* e11105* w11109* e11104* d11112ll l11101* syear   w11107*  


merge 1:1 persnr using "${MY_PATH_IN}epequiv.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr hid  pid persnr d11102ll e11106* d11101* d11106* e11101*  w11105* d11108* e11103* e11102* i11105* i11104* i11103* i11102* i11113* i11101* i11117* i11106* i11107* w11102* i11118* house* hsup* flossc* flossr* fhsup* fhouse* frenty* fjob1* e11201* i11205* i11218* i11209* renty* ijob2* ijob1* iself* kidy* i11110* x11103* x11105* w11101* w11108* lossc* lossr* d11104* d11109* e11105* w11109* e11104* d11112ll l11101* syear   w11107*  

merge 1:1 persnr using "${MY_PATH_IN}fpequiv.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr hid  pid persnr d11102ll e11106* d11101* d11106* e11101*  w11105* d11108* e11103* e11102* i11105* i11104* i11103* i11102* i11113* i11101* i11117* i11106* i11107* w11102* i11118* house* hsup* flossc* flossr* fhsup* fhouse* frenty* fjob1* e11201* i11205* i11218* i11209* renty* ijob1* ijob2* iself* kidy* i11110* x11103* x11105* w11101* w11108* lossc* lossr* d11104* d11109* e11105* w11109* e11104* d11112ll l11101* syear   w11107*  

merge 1:1 persnr using "${MY_PATH_IN}gpequiv.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr  hid pid persnr d11102ll e11106* d11101* d11106* e11101*  w11105* d11108* e11103* e11102* i11105* i11104* i11103* i11102* i11113* i11101* i11117* i11106* i11107* w11102* i11118* house* hsup* flossc* flossr* fhsup* fhouse* frenty* fjob1* e11201* i11205* i11218* i11209* renty* ijob2* ijob1* iself* kidy* i11110* x11103* x11105* w11101* w11108* lossc* lossr* d11104* d11109* e11105* w11109* e11104* d11112ll l11101* syear   w11107*  

merge 1:1 persnr using "${MY_PATH_IN}hpequiv.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr  hid  pid persnr d11102ll e11106* d11101* d11106* e11101*  w11105* d11108* e11103* e11102* i11105* i11104* i11103* i11102* i11113* i11101* i11117* i11106* i11107* w11102* i11118* house* hsup* flossc* flossr* fhsup* fhouse* frenty* fjob1* e11201* i11205* i11218* i11209* renty* ijob1* ijob2* iself* kidy* i11110* x11103* x11105* w11101* w11108* lossc* lossr* d11104* d11109* e11105* w11109* e11104* d11112ll l11101* syear   w11107*  

merge 1:1 persnr using "${MY_PATH_IN}ipequiv.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr hid  pid persnr d11102ll e11106* d11101* d11106* e11101*  w11105* d11108* e11103* e11102* i11105* i11104* i11103* i11102* i11113* i11101* i11117* i11106* i11107* w11102* i11118* house* hsup* flossc* flossr* fhsup* fhouse* frenty* fjob1* e11201* i11205* i11218* i11209* renty* ijob1* ijob2* iself* kidy* i11110* x11103* x11105* w11101* w11108* lossc* lossr* d11104* d11109* e11105* w11109* e11104* d11112ll l11101* syear   w11107*  

merge 1:1 persnr using "${MY_PATH_IN}jpequiv.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr hid  pid persnr d11102ll e11106* d11101* d11106* e11101*  w11105* d11108* e11103* e11102* i11105* i11104* i11103* i11102* i11113* i11101* i11117* i11106* i11107* w11102* i11118* house* hsup* flossc* flossr* fhsup* fhouse* frenty* fjob1* e11201* i11205* i11218* i11209* renty* ijob1* ijob2* iself* kidy* i11110* x11103* x11105* w11101* w11108* lossc* lossr* d11104* d11109* e11105* w11109* e11104* d11112ll l11101* syear   w11107*  

merge 1:1 persnr using "${MY_PATH_IN}kpequiv.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr hid  pid persnr d11102ll e11106* d11101* d11106* e11101*  w11105* d11108* e11103* e11102* i11105* i11104* i11103* i11102* i11113* i11101* i11117* i11106* i11107* w11102* i11118* house* hsup* flossc* flossr* fhsup* fhouse* frenty* fjob1* e11201* i11205* i11218* i11209* renty* ijob1* ijob2* iself* kidy* i11110* x11103* x11105* w11101* w11108* lossc* lossr* d11104* d11109* e11105* w11109* e11104* d11112ll l11101* syear   w11107*  

merge 1:1 persnr using "${MY_PATH_IN}lpequiv.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr hid  pid persnr d11102ll e11106* d11101* d11106* e11101*  w11105* d11108* e11103* e11102* i11105* i11104* i11103* i11102* i11113* i11101* i11117* i11106* i11107* w11102* i11118* house* hsup* flossc* flossr* fhsup* fhouse* frenty* fjob1* e11201* i11205* i11218* i11209* renty* ijob1* ijob2* iself* kidy* i11110* x11103* x11105* w11101* w11108* lossc* lossr* d11104* d11109* e11105* w11109* e11104* d11112ll l11101* syear   w11107*  

merge 1:1 persnr using "${MY_PATH_IN}mpequiv.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr hid  pid persnr d11102ll e11106* d11101* d11106* e11101*  w11105* d11108* e11103* e11102* i11105* i11104* i11103* i11102* i11113* i11101* i11117* i11106* i11107* w11102* i11118* house* hsup* flossc* flossr* fhsup* fhouse* frenty* fjob1* e11201* i11205* i11218* i11209* renty* ijob1* ijob2* iself* kidy* i11110* x11103* x11105* w11101* w11108* lossc* lossr* d11104* d11109* e11105* w11109* e11104* d11112ll l11101* syear   w11107*  

merge 1:1 persnr using "${MY_PATH_IN}npequiv.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr hid  pid persnr d11102ll e11106* d11101* d11106* e11101*  w11105* d11108* e11103* e11102* i11105* i11104* i11103* i11102* i11113* i11101* i11117* i11106* i11107* w11102* i11118* house* hsup* flossc* flossr* fhsup* fhouse* frenty* fjob1* e11201* i11205* i11218* i11209* renty* ijob1* ijob2* iself* kidy* i11110* x11103* x11105* w11101* w11108* lossc* lossr* d11104* d11109* e11105* w11109* e11104* d11112ll l11101* syear   w11107*  

merge 1:1 persnr using "${MY_PATH_IN}opequiv.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr hid  pid persnr d11102ll e11106* d11101* d11106* e11101*  w11105* d11108* e11103* e11102* i11105* i11104* i11103* i11102* i11113* i11101* i11117* i11106* i11107* w11102* i11118* house* hsup* flossc* flossr* fhsup* fhouse* frenty* fjob1* e11201* i11205* i11218* i11209* renty* ijob1* ijob2* iself* kidy* i11110* x11103* x11105* w11101* w11108* lossc* lossr* d11104* d11109* e11105* w11109* e11104* d11112ll l11101* syear   w11107*  

merge 1:1 persnr using "${MY_PATH_IN}ppequiv.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr hid  pid persnr d11102ll e11106* d11101* d11106* e11101*  w11105* d11108* e11103* e11102* i11105* i11104* i11103* i11102* i11113* i11101* i11117* i11106* i11107* w11102* i11118* house* hsup* flossc* flossr* fhsup* fhouse* frenty* fjob1* e11201* i11205* i11218* i11209* renty* ijob1* ijob2* iself* kidy* i11110* x11103* x11105* w11101* w11108* lossc* lossr* d11104* d11109* e11105* w11109* e11104* d11112ll l11101* syear   w11107*  

merge 1:1 persnr using "${MY_PATH_IN}qpequiv.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr hid  pid persnr d11102ll e11106* d11101* d11106* e11101*  w11105* d11108* e11103* e11102* i11105* i11104* i11103* i11102* i11113* i11101* i11117* i11106* i11107* w11102* i11118* house* hsup* flossc* flossr* fhsup* fhouse* frenty* fjob1* e11201* i11205* i11218* i11209* renty* ijob1* ijob2* iself* kidy* i11110* x11103* x11105* w11101* w11108* lossc* lossr* d11104* d11109* e11105* w11109* e11104* d11112ll l11101* syear   w11107*  

merge 1:1 persnr using "${MY_PATH_IN}rpequiv.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr hid  pid persnr d11102ll e11106* d11101* d11106* e11101*  w11105* d11108* e11103* e11102* i11105* i11104* i11103* i11102* i11113* i11101* i11117* i11106* i11107* w11102* i11118* house* hsup* flossc* flossr* fhsup* fhouse* frenty* fjob1* e11201* i11205* i11218* i11209* renty* ijob1* ijob2* iself* kidy* i11110* x11103* x11105* w11101* w11108* lossc* lossr* d11104* d11109* e11105* w11109* e11104* d11112ll l11101* syear   w11107*  

merge 1:1 persnr using "${MY_PATH_IN}spequiv.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr hid  pid persnr d11102ll e11106* d11101* d11106* e11101*  w11105* d11108* e11103* e11102* i11105* i11104* i11103* i11102* i11113* i11101* i11117* i11106* i11107* w11102* i11118* house* hsup* flossc* flossr* fhsup* fhouse* frenty* fjob1* e11201* i11205* i11218* i11209* renty* ijob1* ijob2* iself* kidy* i11110* x11103* x11105* w11101* w11108* lossc* lossr* d11104* d11109* e11105* w11109* e11104* d11112ll l11101* syear   w11107*  

merge 1:1 persnr using "${MY_PATH_IN}tpequiv.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr hid  pid persnr d11102ll e11106* d11101* d11106* e11101*  w11105* d11108* e11103* e11102* i11105* i11104* i11103* i11102* i11113* i11101* i11117* i11106* i11107* w11102* i11118* house* hsup* flossc* flossr* fhsup* fhouse* frenty* fjob1* e11201* i11205* i11218* i11209* renty* ijob1* ijob2* iself* kidy* i11110* x11103* x11105* w11101* w11108* lossc* lossr* d11104* d11109* e11105* w11109* e11104* d11112ll l11101* syear   w11107*  

merge 1:1 persnr using "${MY_PATH_IN}upequiv.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr hid  pid persnr d11102ll e11106* d11101* d11106* e11101*  w11105* d11108* e11103* e11102* i11105* i11104* i11103* i11102* i11113* i11101* i11117* i11106* i11107* w11102* i11118* house* hsup* flossc* flossr* fhsup* fhouse* frenty* fjob1* e11201* i11205* i11218* i11209* renty* ijob1* ijob2* iself* kidy* i11110* x11103* x11105* w11101* w11108* lossc* lossr* d11104* d11109* e11105* w11109* e11104* d11112ll l11101* syear   w11107*  

merge 1:1 persnr using "${MY_PATH_IN}vpequiv.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr hid  pid persnr d11102ll e11106* d11101* d11106* e11101*  w11105* d11108* e11103* e11102* i11105* i11104* i11103* i11102* i11113* i11101* i11117* i11106* i11107* w11102* i11118* house* hsup* flossc* flossr* fhsup* fhouse* frenty* fjob1* e11201* i11205* i11218* i11209* renty* ijob1* ijob2* iself* kidy* i11110* x11103* x11105* w11101* w11108* lossc* lossr* d11104* d11109* e11105* w11109* e11104* d11112ll l11101* syear   w11107*  

merge 1:1 persnr using "${MY_PATH_IN}wpequiv.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr hid  pid persnr d11102ll e11106* d11101* d11106* e11101*  w11105* d11108* e11103* e11102* i11105* i11104* i11103* i11102* i11113* i11101* i11117* i11106* i11107* w11102* i11118* house* hsup* flossc* flossr* fhsup* fhouse* frenty* fjob1* e11201* i11205* i11218* i11209* renty* ijob1* ijob2* iself* kidy* i11110* x11103* x11105* w11101* w11108* lossc* lossr* d11104* d11109* e11105* w11109* e11104* d11112ll l11101* syear   w11107*  

merge 1:1 persnr using "${MY_PATH_IN}xpequiv.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr hid  pid persnr d11102ll e11106* d11101* d11106* e11101*  w11105* d11108* e11103* e11102* i11105* i11104* i11103* i11102* i11113* i11101* i11117* i11106* i11107* w11102* i11118* house* hsup* flossc* flossr* fhsup* fhouse* frenty* fjob1* e11201* i11205* i11218* i11209* renty* ijob1* ijob2* iself* kidy* i11110* x11103* x11105* w11101* w11108* lossc* lossr* d11104* d11109* e11105* w11109* e11104* d11112ll l11101* syear   w11107*  

merge 1:1 persnr using "${MY_PATH_IN}ypequiv.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr hid  pid persnr d11102ll e11106* d11101* d11106* e11101*  w11105* d11108* e11103* e11102* i11105* i11104* i11103* i11102* i11113* i11101* i11117* i11106* i11107* w11102* i11118* house* hsup* flossc* flossr* fhsup* fhouse* frenty* fjob1* e11201* i11205* i11218* i11209* renty* ijob1* ijob2* iself* kidy* i11110* x11103* x11105* w11101* w11108* lossc* lossr* d11104* d11109* e11105* w11109* e11104* d11112ll l11101* syear   w11107*  

merge 1:1 persnr using "${MY_PATH_IN}zpequiv.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr hid  pid persnr d11102ll e11106* d11101* d11106* e11101*  w11105* d11108* e11103* e11102* i11105* i11104* i11103* i11102* i11113* i11101* i11117* i11106* i11107* w11102* i11118* house* hsup* flossc* flossr* fhsup* fhouse* frenty* fjob1* e11201* i11205* i11218* i11209* renty* ijob1* ijob2* iself* kidy* i11110* x11103* x11105* w11101* w11108* lossc* lossr* d11104* d11109* e11105* w11109* e11104* d11112ll l11101* syear   w11107*  

merge 1:1 persnr using "${MY_PATH_IN}bapequiv.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr bahhnr hid  pid persnr d11102ll e11106* d11101* d11106* e11101*  w11105* d11108* e11103* e11102* i11105* i11104* i11103* i11102* i11113* i11101* i11117* i11106* i11107* w11102* i11118* house* hsup* flossc* flossr* fhsup* fhouse* frenty* fjob1* e11201* i11205* i11218* i11209* renty* ijob1* ijob2* iself* kidy* i11110* x11103* x11105* w11101* w11108* lossc* lossr* d11104* d11109* e11105* w11109* e11104* d11112ll l11101* syear   w11107*  

merge 1:1 persnr using "${MY_PATH_IN}bbpequiv.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr bahhnr bbhhnr hid  pid persnr d11102ll e11106* d11101* d11106* e11101*  w11105* d11108* e11103* e11102* i11105* i11104* i11103* i11102* i11113* i11101* i11117* i11106* i11107* w11102* i11118* house* hsup* flossc* flossr* fhsup* fhouse* frenty* fjob1* e11201* i11205* i11218* i11209* renty* ijob1* ijob2* iself* kidy* i11110* x11103* x11105* w11101* w11108* lossc* lossr* d11104* d11109* e11105* w11109* e11104* d11112ll l11101* syear   w11107* 

merge 1:1 persnr using "${MY_PATH_IN}bcpequiv.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr bahhnr bbhhnr bchhnr hid  pid persnr d11102ll e11106* d11101* d11106* e11101*  w11105* d11108* e11103* e11102* i11105* i11104* i11103* i11102* i11113* i11101* i11117* i11106* i11107* w11102* i11118* house* hsup* flossc* flossr* fhsup* fhouse* frenty* fjob1* e11201* i11205* i11218* i11209* renty* ijob1* ijob2* iself* kidy* i11110* x11103* x11105* w11101* w11108* lossc* lossr* d11104* d11109* e11105* w11109* e11104* d11112ll l11101* syear   w11107* 

merge 1:1 persnr using "${MY_PATH_IN}bdpequiv.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr bahhnr bbhhnr bchhnr bdhhnr hid  pid persnr d11102ll e11106* d11101* d11106* e11101*  w11105* d11108* e11103* e11102* i11105* i11104* i11103* i11102* i11113* i11101* i11117* i11106* i11107* w11102* i11118* house* hsup* flossc* flossr* fhsup* fhouse* frenty* fjob1* e11201* i11205* i11218* i11209* renty* ijob1* ijob2* iself* kidy* i11110* x11103* x11105* w11101* w11108* lossc* lossr* d11104* d11109* e11105* w11109* e11104* d11112ll l11101* syear   w11107* 

merge 1:1 persnr using "${MY_PATH_IN}bepequiv.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr bahhnr bbhhnr bchhnr bdhhnr behhnr hid  pid persnr d11102ll e11106* d11101* d11106* e11101*  w11105* d11108* e11103* e11102* i11105* i11104* i11103* i11102* i11113* i11101* i11117* i11106* i11107* w11102* i11118* house* hsup* flossc* flossr* fhsup* fhouse* frenty* fjob1* e11201* i11205* i11218* i11209* renty* ijob1* ijob2* iself* kidy* i11110* x11103* x11105* w11101* w11108* lossc* lossr* d11104* d11109* e11105* w11109* e11104* d11112ll l11101* syear   w11107* 

merge 1:1 persnr using "${MY_PATH_IN}bfpequiv.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr bahhnr bbhhnr bchhnr bdhhnr behhnr bfhhnr hid  pid persnr d11102ll e11106* d11101* d11106* e11101*  w11105* d11108* e11103* e11102* i11105* i11104* i11103* i11102* i11113* i11101* i11117* i11106* i11107* w11102* i11118* house* hsup* flossc* flossr* fhsup* fhouse* frenty* fjob1* e11201* i11205* i11218* i11209* renty* ijob1* ijob2* iself* kidy* i11110* x11103* x11105* w11101* w11108* lossc* lossr* d11104* d11109* e11105* w11109* e11104* d11112ll l11101* syear   w11107* 

merge 1:1 persnr using "${MY_PATH_IN}bgpequiv.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr bahhnr bbhhnr bchhnr bdhhnr behhnr bfhhnr bghhnr hid  pid persnr d11102ll e11106* d11101* d11106* e11101*  w11105* d11108* e11103* e11102* i11105* i11104* i11103* i11102* i11113* i11101* i11117* i11106* i11107* w11102* i11118* house* hsup* flossc* flossr* fhsup* fhouse* frenty* fjob1* e11201* i11205* i11218* i11209* renty* ijob1* ijob2* iself* kidy* i11110* x11103* x11105* w11101* w11108* lossc* lossr* d11104* d11109* e11105* w11109* e11104* d11112ll   l11101* syear   w11107* 

merge 1:1 persnr using "${MY_PATH_IN}bhpequiv.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr bahhnr bbhhnr bchhnr bdhhnr behhnr bfhhnr bghhnr bhhhnr hid  pid persnr d11102ll e11106* d11101* d11106* e11101*  w11105* d11108* e11103* e11102* i11105* i11104* i11103* i11102* i11113* i11101* i11117* i11106* i11107* w11102* i11118* house* hsup* flossc* flossr* fhsup* fhouse* frenty* fjob1* e11201* i11205* i11218* i11209* renty* ijob1* ijob2* iself* kidy* i11110* x11103* x11105* w11101* w11108* lossc* lossr* d11104* d11109* e11105* w11109* e11104* d11112ll l11101* syear   w11107* 

merge 1:1 persnr using "${MY_PATH_IN}bipequiv.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr bahhnr bbhhnr bchhnr bdhhnr behhnr bfhhnr bghhnr bhhhnr bihhnr hid  pid persnr d11102ll e11106* d11101* d11106* e11101*  w11105* d11108* e11103* e11102* i11105* i11104* i11103* i11102* i11113* i11101* i11117* i11106* i11107* w11102* i11118* house* hsup* flossc* flossr* fhsup* fhouse* frenty* fjob1* e11201* i11205* i11218* i11209* renty* ijob1* ijob2* iself* kidy* i11110* x11103* x11105* w11101* w11108* lossc* lossr* d11104* d11109* e11105* w11109* e11104* d11112ll l11101* syear   w11107* 

save "/Users/[AUTHOR]/Desktop/Housing Project/SOEP/equiv_master.dta", replace

***C.MERGE THE POLITICAL AND ADDITIONAL EMPLOYMENT VARIABLES FROM THE PERSONAL FILES***

use "${MY_PATH_IN}ap.dta"
merge 1:1 persnr using"${MY_PATH_IN}bp.dta"
drop _merge

gen party_preference1984 = ap5602
gen party_preference1985 = bp7902

gen net_p_income1984 = ap3302
gen net_p_income1985 = bp4302

keep ahhnr bhhnr hid  pid persnr party_preference* net_p_income*  

merge 1:1 persnr using"${MY_PATH_IN}cp.dta"
drop _merge

gen party_preference1986 = cp7902
gen net_p_income1986 = cp5202

keep ahhnr bhhnr chhnr hid  pid persnr party_preference* net_p_income*  

merge 1:1 persnr using"${MY_PATH_IN}dp.dta"
drop _merge

gen party_preference1987 = dp8802
gen net_p_income1987 = dp4402

keep ahhnr bhhnr chhnr dhhnr hid  pid persnr party_preference* net_p_income*  

merge 1:1 persnr using"${MY_PATH_IN}ep.dta"
drop _merge

gen party_preference1988 = ep7702
gen net_p_income1988 = ep4402

keep ahhnr bhhnr chhnr dhhnr ehhnr hid  pid persnr party_preference* net_p_income*  

merge 1:1 persnr using"${MY_PATH_IN}fp.dta"
drop _merge

gen party_preference1989 = fp9302
gen net_p_income1989 = fp4502

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr hid  pid persnr party_preference* net_p_income*  

merge 1:1 persnr using"${MY_PATH_IN}gp.dta"
drop _merge

gen party_preference1990 = gp8502
gen net_p_income1990 = gp4302

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr  hid  pid persnr party_preference* net_p_income*  

merge 1:1 persnr using"${MY_PATH_IN}hp.dta"
drop _merge

gen party_preference1991 = hp9002
gen net_p_income1991 = hp5402

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr  hid  pid persnr party_preference* net_p_income*  

merge 1:1 persnr using"${MY_PATH_IN}ip.dta"
drop _merge

gen party_preference1992 = ip9002
gen net_p_income1992 = ip5402

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr hid  pid persnr party_preference* net_p_income*  

merge 1:1 persnr using"${MY_PATH_IN}jp.dta"
drop _merge

gen party_preference1993 = jp9002
gen net_p_income1993 = jp5402

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr hid  pid persnr party_preference* net_p_income*  

merge 1:1 persnr using"${MY_PATH_IN}kp.dta"
drop _merge

gen party_preference1994 = kp9202
gen net_p_income1994 = kp6402

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr hid  pid persnr party_preference* net_p_income*  

merge 1:1 persnr using"${MY_PATH_IN}lp.dta"
drop _merge

gen party_preference1995 = lp9802
gen net_p_income1995 = lp5302

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr hid  pid persnr party_preference* net_p_income*  

merge 1:1 persnr using"${MY_PATH_IN}mp.dta"
drop _merge

gen party_preference1996 = mp8402
gen net_p_income1996 = mp4702

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr hid  pid persnr party_preference* net_p_income*  

merge 1:1 persnr using"${MY_PATH_IN}np.dta"
drop _merge

gen party_preference1997 = np9402
gen net_p_income1997 = np5402

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr hid  pid persnr party_preference* net_p_income*  

merge 1:1 persnr using"${MY_PATH_IN}op.dta"
drop _merge

gen party_preference1998 = op9702
gen net_p_income1998 = op4502

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr hid  pid persnr party_preference* net_p_income*  

merge 1:1 persnr using"${MY_PATH_IN}pp.dta"
drop _merge

gen party_preference1999 = pp11201
gen net_p_income1999 = pp6002

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr hid  pid persnr party_preference* net_p_income*  

merge 1:1 persnr using"${MY_PATH_IN}qp.dta"
drop _merge

gen party_preference2000 = qp11701
gen net_p_income2000 = qp5602

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr hid  pid persnr party_preference* net_p_income*  

merge 1:1 persnr using"${MY_PATH_IN}rp.dta"
drop _merge

gen party_preference2001 = rp112
gen net_p_income2001 = rp5702

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr hid  pid persnr party_preference* net_p_income*  

merge 1:1 persnr using"${MY_PATH_IN}sp.dta"
drop _merge

gen party_preference2002 = sp11201
gen net_p_income2002 = sp5802

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr hid  pid persnr party_preference* net_p_income*  

merge 1:1 persnr using"${MY_PATH_IN}tp.dta"
drop _merge

gen party_preference2003 = tp11901
gen net_p_income2003 = tp7602

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr hid  pid persnr party_preference* net_p_income*  

merge 1:1 persnr using"${MY_PATH_IN}up.dta"
drop _merge

gen party_preference2004 = up12401
gen net_p_income2004 = up5902

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr hid  pid persnr party_preference* net_p_income*  

merge 1:1 persnr using"${MY_PATH_IN}vp.dta"
drop _merge

gen party_preference2005 = vp13001
gen net_p_income2005 = vp7102

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr hid  pid persnr party_preference* net_p_income*  

merge 1:1 persnr using"${MY_PATH_IN}wp.dta"
drop _merge

gen party_preference2006 = wp12001
gen net_p_income2006 = wp5902

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr hid  pid persnr party_preference* net_p_income*  

merge 1:1 persnr using"${MY_PATH_IN}xp.dta"
drop _merge

gen party_preference2007 = xp12901
gen net_p_income2007 = xp7302

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr hid  pid persnr party_preference* net_p_income*  

merge 1:1 persnr using"${MY_PATH_IN}yp.dta"
drop _merge

gen party_preference2008 = yp13101
gen net_p_income2008 = yp6802

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr hid  pid persnr party_preference* net_p_income*  

merge 1:1 persnr using"${MY_PATH_IN}zp.dta"
drop _merge

gen party_preference2009 = zp12401
gen net_p_income2009 = zp7202

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr hid  pid persnr party_preference* net_p_income*  

merge 1:1 persnr using"${MY_PATH_IN}bap.dta"
drop _merge

gen party_preference2010 = bap12901
gen net_p_income2010 = bap6102

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr bahhnr hid  pid persnr party_preference* net_p_income*  

merge 1:1 persnr using"${MY_PATH_IN}bbp.dta"
drop _merge

gen party_preference2011 = bbp13001
gen net_p_income2011 = bbp7302

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr bahhnr bbhhnr hid  pid persnr party_preference* net_p_income* 

merge 1:1 persnr using"${MY_PATH_IN}bcp.dta"
drop _merge

gen party_preference2012 = bcp12601
gen net_p_income2012 = bcp5902

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr bahhnr bbhhnr bchhnr hid  pid persnr party_preference* net_p_income* 

merge 1:1 persnr using"${MY_PATH_IN}bdp.dta"
drop _merge

gen party_preference2013 = bdp13201
gen net_p_income2013 = bdp7702

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr bahhnr bbhhnr bchhnr bdhhnr hid  pid persnr party_preference* net_p_income* 

merge 1:1 persnr using"${MY_PATH_IN}bep.dta"
drop _merge

gen party_preference2014 = bep12001
gen net_p_income2014 = bep5702
gen party_voted2014 = bep121

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr bahhnr bbhhnr bchhnr bdhhnr behhnr hid  pid persnr party_preference* net_p_income* party_voted*

merge 1:1 persnr using"${MY_PATH_IN}bfp.dta"
drop _merge

gen party_preference2015 = bfp14501
gen net_p_income2015 = bfp10202

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr bahhnr bbhhnr bchhnr bdhhnr behhnr bfhhnr hid  pid persnr party_preference* net_p_income* party_voted* 

merge 1:1 persnr using"${MY_PATH_IN}bgp.dta"
drop _merge

gen party_preference2016 = bgp14501
gen net_p_income2016 = bgp8102

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr bahhnr bbhhnr bchhnr bdhhnr behhnr bfhhnr bghhnr hid  pid persnr party_preference* net_p_income* party_voted* 

merge 1:1 persnr using"${MY_PATH_IN}bhp.dta"
drop _merge

gen party_preference2017 = bhp_184_01
gen net_p_income2017 = bhp_97_02

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr bahhnr bbhhnr bchhnr bdhhnr behhnr bfhhnr bghhnr bhhhnr hid  pid persnr party_preference* net_p_income* party_voted* 

merge 1:1 persnr using"${MY_PATH_IN}bip.dta"
drop _merge

gen party_preference2018 = bip_173_01
gen net_p_income2018 = bip_97_02
gen party_voted2018 = bip_175

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr bahhnr bbhhnr bchhnr bdhhnr behhnr bfhhnr bghhnr bhhhnr bihhnr hid  pid persnr party_preference* net_p_income* party_voted* 

save "/Users/[AUTHOR]/Desktop/Housing Project/SOEP/p_master.dta", replace

***D.MERGE ADDITIONAL EMPLOYMENT FILES FROM THE PERSONAL GENERATED FILES***

use "${MY_PATH_IN}apgen.dta"
merge 1:1 persnr using"${MY_PATH_IN}bpgen.dta"
drop _merge

keep ahhnr bhhnr hid  pid persnr lfs* labnet* labgro* 

merge 1:1 persnr using"${MY_PATH_IN}cpgen.dta"
drop _merge

keep ahhnr bhhnr chhnr hid  pid persnr lfs* labnet* labgro* 

merge 1:1 persnr using"${MY_PATH_IN}dpgen.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr hid  pid persnr lfs* labnet* labgro* 

merge 1:1 persnr using"${MY_PATH_IN}epgen.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr hid  pid persnr lfs* labnet* labgro* 

merge 1:1 persnr using"${MY_PATH_IN}fpgen.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr hid  pid persnr lfs* labnet* labgro* 

merge 1:1 persnr using"${MY_PATH_IN}gpgen.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr  hid  pid persnr lfs* labnet* labgro* 

merge 1:1 persnr using"${MY_PATH_IN}hpgen.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr  hid  pid persnr lfs* labnet* labgro* 

merge 1:1 persnr using"${MY_PATH_IN}ipgen.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr hid  pid persnr lfs* labnet* labgro* 

merge 1:1 persnr using"${MY_PATH_IN}jpgen.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr hid  pid persnr lfs* labnet* labgro* 

merge 1:1 persnr using"${MY_PATH_IN}kpgen.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr hid  pid persnr lfs* labnet* labgro* 

merge 1:1 persnr using"${MY_PATH_IN}lpgen.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr hid  pid persnr lfs* labnet* labgro* 

merge 1:1 persnr using"${MY_PATH_IN}mpgen.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr hid  pid persnr lfs* labnet* labgro* 

merge 1:1 persnr using"${MY_PATH_IN}npgen.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr hid  pid persnr lfs* labnet* labgro* 

merge 1:1 persnr using"${MY_PATH_IN}opgen.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr hid  pid persnr lfs* labnet* labgro* 

merge 1:1 persnr using"${MY_PATH_IN}ppgen.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr hid  pid persnr lfs* labnet* labgro* 

merge 1:1 persnr using"${MY_PATH_IN}qpgen.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr hid  pid persnr lfs* labnet* labgro* 

merge 1:1 persnr using"${MY_PATH_IN}rpgen.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr hid  pid persnr lfs* labnet* labgro* 

merge 1:1 persnr using"${MY_PATH_IN}spgen.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr hid  pid persnr lfs* labnet* labgro* 

merge 1:1 persnr using"${MY_PATH_IN}tpgen.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr hid  pid persnr lfs* labnet* labgro* 

merge 1:1 persnr using"${MY_PATH_IN}upgen.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr hid  pid persnr lfs* labnet* labgro* 

merge 1:1 persnr using"${MY_PATH_IN}vpgen.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr hid  pid persnr lfs* labnet* labgro* 

merge 1:1 persnr using"${MY_PATH_IN}wpgen.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr hid  pid persnr lfs* labnet* labgro* 

merge 1:1 persnr using"${MY_PATH_IN}xpgen.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr hid  pid persnr lfs* labnet* labgro* 

merge 1:1 persnr using"${MY_PATH_IN}ypgen.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr hid  pid persnr lfs* labnet* labgro* 

merge 1:1 persnr using"${MY_PATH_IN}zpgen.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr hid  pid persnr lfs* labnet* labgro* 

merge 1:1 persnr using"${MY_PATH_IN}bapgen.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr bahhnr hid  pid persnr lfs* labnet* labgro* 

merge 1:1 persnr using"${MY_PATH_IN}bbpgen.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr bahhnr bbhhnr hid  pid persnr lfs* labnet* labgro*

merge 1:1 persnr using"${MY_PATH_IN}bcpgen.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr bahhnr bbhhnr bchhnr hid  pid persnr lfs* labnet* labgro*

merge 1:1 persnr using"${MY_PATH_IN}bdpgen.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr bahhnr bbhhnr bchhnr bdhhnr hid  pid persnr lfs* labnet* labgro*

merge 1:1 persnr using"${MY_PATH_IN}bepgen.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr bahhnr bbhhnr bchhnr bdhhnr behhnr hid  pid persnr lfs* labnet* labgro*

merge 1:1 persnr using"${MY_PATH_IN}bfpgen.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr bahhnr bbhhnr bchhnr bdhhnr behhnr bfhhnr hid  pid persnr lfs* labnet* labgro*

merge 1:1 persnr using"${MY_PATH_IN}bgpgen.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr bahhnr bbhhnr bchhnr bdhhnr behhnr bfhhnr bghhnr hid  pid persnr lfs* labnet* labgro*

merge 1:1 persnr using"${MY_PATH_IN}bhpgen.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr bahhnr bbhhnr bchhnr bdhhnr behhnr bfhhnr bghhnr bhhhnr hid  pid persnr lfs* labnet* labgro*

merge 1:1 persnr using"${MY_PATH_IN}bipgen.dta"
drop _merge

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr bahhnr bbhhnr bchhnr bdhhnr behhnr bfhhnr bghhnr bhhhnr bihhnr hid  pid persnr lfs* labnet* labgro*

save "/Users/[AUTHOR]/Desktop/Housing Project/SOEP/pgen_master.dta", replace

***E.MERGE RENT VARIABLE FROM THE HH FILES****

clear

use "${MY_PATH_IN}ah.dta"
merge 1:1 hid using"${MY_PATH_IN}bh.dta"
drop _merge

gen monthly_rent1984 = ah1901
gen monthly_rent1985 = bh21g01

keep ahhnr bhhnr hid monthly_rent*

merge 1:1 hid using"${MY_PATH_IN}ch.dta"
drop _merge

gen monthly_rent1986 = ch26g01

keep ahhnr bhhnr chhnr hid monthly_rent*

merge 1:1 hid using"${MY_PATH_IN}dh.dta"
drop _merge

gen monthly_rent1987 = dh19g01

keep ahhnr bhhnr chhnr dhhnr hid monthly_rent*

merge 1:1 hid using"${MY_PATH_IN}eh.dta"
drop _merge

gen monthly_rent1988 = eh10g01

keep ahhnr bhhnr chhnr dhhnr ehhnr hid monthly_rent*

merge 1:1 hid using"${MY_PATH_IN}fh.dta"
drop _merge

gen monthly_rent1989 = fh10g01

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr hid monthly_rent*

merge 1:1 hid using"${MY_PATH_IN}gh.dta"
drop _merge

gen monthly_rent1990 = gh10g01

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr  hid monthly_rent*

merge 1:1 hid using"${MY_PATH_IN}hh.dta"
drop _merge

gen monthly_rent1991 = hh2601

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr  hid monthly_rent*

merge 1:1 hid using"${MY_PATH_IN}ih.dta"
drop _merge

gen monthly_rent1992 = ih2601

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr hid monthly_rent*

merge 1:1 hid using"${MY_PATH_IN}jh.dta"
drop _merge

gen monthly_rent1993 = jh2601

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr hid monthly_rent*

merge 1:1 hid using"${MY_PATH_IN}kh.dta"
drop _merge

gen monthly_rent1994 = kh2601

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr hid monthly_rent*

merge 1:1 hid using"${MY_PATH_IN}lh.dta"
drop _merge

gen monthly_rent1995 = lh2601

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr hid monthly_rent*

merge 1:1 hid using"${MY_PATH_IN}mh.dta"
drop _merge

gen monthly_rent1996 = mh2701

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr hid monthly_rent*

merge 1:1 hid using"${MY_PATH_IN}nh.dta"
drop _merge

gen monthly_rent1997 = nh2701

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr hid monthly_rent*

merge 1:1 hid using"${MY_PATH_IN}oh.dta"
drop _merge

gen monthly_rent1998 = oh2701

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr hid monthly_rent*

merge 1:1 hid using"${MY_PATH_IN}ph.dta"
drop _merge

gen monthly_rent1999 = ph2601

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr hid monthly_rent*

merge 1:1 hid using"${MY_PATH_IN}qh.dta"
drop _merge

gen monthly_rent2000 = qh2501

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr hid monthly_rent*

merge 1:1 hid using"${MY_PATH_IN}rh.dta"
drop _merge

gen monthly_rent2001 = rh2501

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr hid monthly_rent*

merge 1:1 hid using"${MY_PATH_IN}sh.dta"
drop _merge

gen monthly_rent2002 = sh2501

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr hid monthly_rent*

merge 1:1 hid using"${MY_PATH_IN}th.dta"
drop _merge

gen monthly_rent2003 = th2301

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr hid monthly_rent*

merge 1:1 hid using"${MY_PATH_IN}uh.dta"
drop _merge

gen monthly_rent2004 = uh2301

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr hid monthly_rent*

merge 1:1 hid using"${MY_PATH_IN}vh.dta"
drop _merge

gen monthly_rent2005 = vh2201

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr hid monthly_rent*

merge 1:1 hid using"${MY_PATH_IN}wh.dta"
drop _merge

gen monthly_rent2006 = wh2201

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr hid monthly_rent*

merge 1:1 hid using"${MY_PATH_IN}xh.dta"
drop _merge

gen monthly_rent2007 = xh2201

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr hid monthly_rent*

merge 1:1 hid using"${MY_PATH_IN}yh.dta"
drop _merge

gen monthly_rent2008 = yh2301

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr hid monthly_rent*

merge 1:1 hid using"${MY_PATH_IN}zh.dta"
drop _merge

gen monthly_rent2009 = zh2301

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr hid monthly_rent*

merge 1:1 hid using"${MY_PATH_IN}bah.dta"
drop _merge

gen monthly_rent2010 = bah2301

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr bahhnr hid monthly_rent*

merge 1:1 hid using"${MY_PATH_IN}bbh.dta"
drop _merge

gen monthly_rent2011 = bbh2301

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr bahhnr bbhhnr hid monthly_rent*

merge 1:1 hid using"${MY_PATH_IN}bch.dta"
drop _merge

gen monthly_rent2012 = bch2301

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr bahhnr bbhhnr bchhnr hid monthly_rent*

merge 1:1 hid using"${MY_PATH_IN}bdh.dta"
drop _merge

gen monthly_rent2013 = bdh2301

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr bahhnr bbhhnr bchhnr bdhhnr hid monthly_rent*

merge 1:1 hid using"${MY_PATH_IN}beh.dta"
drop _merge

gen monthly_rent2014 = beh2401

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr bahhnr bbhhnr bchhnr bdhhnr behhnr hid monthly_rent*

merge 1:1 hid using"${MY_PATH_IN}bfh.dta"
drop _merge

gen monthly_rent2015 = bfh3401

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr bahhnr bbhhnr bchhnr bdhhnr behhnr bfhhnr hid monthly_rent*

merge 1:1 hid using"${MY_PATH_IN}bgh.dta"
drop _merge

gen monthly_rent2016 = bgh3401

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr bahhnr bbhhnr bchhnr bdhhnr behhnr bfhhnr bghhnr hid monthly_rent*

merge 1:1 hid using"${MY_PATH_IN}bhh.dta"
drop _merge

gen monthly_rent2017 = bhh_30_01

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr bahhnr bbhhnr bchhnr bdhhnr behhnr bfhhnr bghhnr bhhhnr hid monthly_rent*

merge 1:1 hid using"${MY_PATH_IN}bih.dta"
drop _merge

gen monthly_rent2018 = bih_30_01

keep ahhnr bhhnr chhnr dhhnr ehhnr fhhnr ghhnr hhhnr ihhnr jhhnr khhnr lhhnr mhhnr nhhnr ohhnr phhnr qhhnr rhhnr shhnr thhnr uhhnr vhhnr whhnr xhhnr yhhnr zhhnr bahhnr bbhhnr bchhnr bdhhnr behhnr bfhhnr bghhnr bhhhnr bihhnr hid monthly_rent*

save "/Users/[AUTHOR]/Desktop/Housing Project/SOEP/h_master.dta", replace

*F. CREATE A MASTER FILE*

use   "${MY_PATH_OUT}master.dta", clear
merge 1:1 persnr using "/Users/[AUTHOR]/Desktop/Housing Project/SOEP/equiv_master.dta"
drop _merge

merge 1:1 persnr using "/Users/[AUTHOR]/Desktop/Housing Project/SOEP/p_master.dta"
drop _merge 

*drop east-west var*
drop *_ew

*drop variables denoting flags for imputed values --  note: I couldnt keep these because of size restrictions, can be added if needed*
drop flossc* flossr* fhsup* fhouse* frenty* fjob1*

merge 1:1 persnr using "/Users/[AUTHOR]/Desktop/Housing Project/SOEP/pgen_master.dta"
drop _merge

****WIDE TO LONG****
local y=1984
	foreach a in 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 {
		rename e11106`a' e11106`y'
		rename d11101`a' d11101`y'
		rename d11106`a' d11106`y'
		rename e11101`a' e11101`y'
		rename w11105`a' w11105`y'
		rename d11108`a' d11108`y'
		rename e11103`a' e11103`y'
		rename e11102`a' e11102`y'
		rename i11105`a' i11105`y'

		rename i11104`a' i11104`y'
		rename i11103`a' i11103`y'
		rename i11102`a' i11102`y'
		rename i11113`a' i11113`y'
		rename i11101`a' i11101`y'
		rename i11117`a' i11117`y'
		rename i11106`a' i11106`y'
		rename i11107`a' i11107`y'
		rename w11102`a' w11102`y'
		
		rename i11118`a' i11118`y'
		rename house`a' house`y'
		rename hsup`a' hsup`y'
		rename e11201`a' e11201`y'	
		
		rename i11205`a' i11205`y'
		rename i11218`a' i11218`y'
		rename i11209`a' i11209`y'
		rename renty`a' renty`y'
		rename ijob2`a' ijob2`y'
		rename ijob1`a' ijob1`y'
		rename iself`a' iself`y'
		rename kidy`a' kidy`y'
		rename i11110`a' i11110`y'
		rename x11103`a' x11103`y'
		rename x11105`a' x11105`y'
		
		rename w11101`a' w11101`y'
		rename w11108`a' w11108`y'
		rename lossc`a' lossc`y'
		rename lossr`a' lossr`y'
		rename d11104`a' d11104`y'
		rename d11109`a' d11109`y'
		rename e11105`a' e11105`y'
		rename w11109`a' w11109`y'
		rename e11104`a' e11104`y'
		rename l11101`a' l11101`y'
		rename w11107`a' w11107`y'
		rename labnet`a' labnet`y'
		rename labgro`a' labgro`y'
		rename lfs`a' lfs`y'
		local y=`y'+1
		}
				
local y=1984
	foreach a in a b c d e f g h i j k l m n o p q r s t u v w x y z ba bb bc bd be bf bg bh bi {
		rename `a'hhnr hhnrakt`y'
		rename `a'netto netto`y'
		rename `a'pop pop`y'
		rename `a'phrf phrf`y'
		local y=`y'+1
		}
					
save "/Users/[AUTHOR]/Desktop/Housing Project/SOEP/soep_master.dta", replace

reshape long netto pop phrf hhnrakt e11106 d11101 d11106 e11101 w11105 d11108 e11103 e11102  i11105 i11104 i11103 i11102 i11113 i11101 i11117 i11106 i11107 w11102 i11118 house hsup  e11201 i11205 i11218 i11209 renty ijob2 ijob1 iself kidy i11110 x11103 x11105 w11101 w11108 lossc lossr d11104 d11109 e11105 w11109 e11104 l11101 w11107 party_preference party_voted net_p_income labnet labgro lfs, i(persnr) j(year)

save "/Users/[AUTHOR]/Desktop/Housing Project/SOEP/soep_master_reshaped.dta", replace

use "/Users/[AUTHOR]/Desktop/Housing Project/SOEP/h_master.dta", clear
	
local y=1984
	foreach a in a b c d e f g h i j k l m n o p q r s t u v w x y z ba bb bc bd be bf bg bh bi {
		rename `a'hhnr hhnrakt`y'
		local y=`y'+1
		}
		
reshape long hhnrakt monthly_rent, i(hid) j(year)

save "/Users/[AUTHOR]/Desktop/Housing Project/SOEP/h_master_reshaped.dta", replace
 
use "/Users/[AUTHOR]/Desktop/Housing Project/SOEP/soep_master_reshaped.dta", clear
   
merge m:1 hid year using "/Users/[AUTHOR]/Desktop/Housing Project/SOEP/h_master_reshaped.dta"
drop _merge

****DROP OBSERVATIONS FOR WAVES INDIVIDUALS DID NOT PARTICIPATE IN****

drop if hhnrakt == -2

save "/Users/[AUTHOR]/Desktop/Housing Project/SOEP/soep_master_long.dta", replace
	
***MERGE WITH INFO ON REGION (URBAN-RURAL) FROM A HH BRUTTO FILE***

use "/Users/[AUTHOR]/Desktop/Housing Project/SOEP/Stata/hbrutto.dta", clear

sort hid syear

keep hid syear regtyp 
rename syear year
sort hid year

save "/Users/[AUTHOR]/Desktop/Housing Project/SOEP/region_type.dta", replace

use "/Users/[AUTHOR]/Desktop/Housing Project/SOEP/soep_master_long.dta", replace

merge m:1 hid year using "/Users/[AUTHOR]/Desktop/Housing Project/SOEP/region_type.dta"
drop _merge

****label variables****

label variable e11106 "industry code (person's employment)"
label variable d11101 "age"
label variable e11101 "annual work hours"
label variable netto "survey status - befragungsstatus"
label variable pop "populatioinszugehoerigkeit"
label variable phrf "hochrechnungsfaktor"
label variable w11105 "cross-sectional weight - all samples"
label variable party_preference "party supported"
label variable d11108 "education with respect to high school"
label variable e11103 "employment level of individual"
label variable e11102 "employment status of individual"
label variable gebjahr "birth year" 
label variable corigin "country of origin - geburtsland"
label variable d11102ll "gender of individual"
label variable i11105 "hh imputed rent"
label variable i11104 "income from asset flows"
label variable i11103 "hh labor income"
label variable i11102 "hh post-govt income"
label variable i11113 "hh post-govt income (taxsim)" 
label variable i11101 "hh pre-govt income"
label variable i11117 "hh private retirement income"
label variable i11106 "hh private transfers"
label variable i11107 "hh public trasnfers"
label variable w11102 "hh weight"
label variable hhnrakt "haushaltnummer"
label variable i11118 "household windfall income"
label variable house "housing benefit"
label variable hsup "housing support for owner-occupiers"
label variable e11201 "impute annual work hours of individual"
label variable i11205 "impute hh imputed rental value"
label variable i11218 "impute hh windfall income"
label variable i11209 "impute total hh taxes"
label variable renty "income from rental and leasing"
label variable ijob2 "income from secondary employment"
label variable iself "income from self-employment"
label variable kidy "income of kids in hh"
label variable i11110 "individual labor earnings"
label variable x11103 "individual in hh at survey"
label variable x11105 "individual responded to survey"
label variable w11101 "individuals cross-sectional weight -- without 1st wave of a subsample"
label variable w11108 "longitudinal weight - enumerated individual"
label variable lossc "losses from capital investment"
label variable lossr "losses from renting and leasing"
label variable d11104 "marital status"
label variable d11109 "number of years in education"
label variable e11105 "occupation of individual"
label variable w11109 "population factor for w11103$"
label variable e11104 "primary activity of individual"
label variable l11101 "state of residence"
label variable w11107 "x-sectional weight"
label variable d11106 "number of persons in hh"
label variable ijob1 "wages, salary from main job"
label variable net_p_income "net personal income"
label variable lfs "labor force status"
label variable labnet "current net labor income, euro" 
label variable labgro "current gross labor income, euro" 
label variable monthly_rent "monthly rent"
label variable party_voted "party voted, bundestag"
label variable year "year"

*HOW MANY WAVES DID INDIVIDUALS PARTICIPATE IN?*

bys persnr: gen waves=_N		/*additional information: count number of waves per person*/
tab waves

sort pid year 

save "/Users/[AUTHOR]/Desktop/Housing Project/SOEP/soep_master_final", replace

****ENCODE MISSING VALUES****
*mvdecode _all, mv(-1=. \ -2=.t \ -3=.x \ -5=.y \ -8=.z)



* Inclusion of household variables
use "/Users/[AUTHOR]/Desktop/Housing Project/SOEP/hl.dta", clear
keep hid syear hlf0107_h hlf0106 hlf0011_h hlc0084_h
* For documentation on variable names and their values, see: http://companion.soep.de/Topics%20of%20SOEPcore/Home%2C%20Amenities%2C%20and%20Contributions%20of%20Private%20HH.html
rename syear year
merge 1:m hid year using "/Users/[AUTHOR]/Desktop/Housing Project/SOEP/soep_master_final.dta"
drop if pid==.
drop syear
drop if _merge==1
drop _merge
rename year syear
save "/Users/[AUTHOR]/Desktop/Housing Project/SOEP/soep_master_final_nomissingPID.dta", replace

use pid syear h11101 using "/Users/[AUTHOR]/Desktop/Housing Project/SOEP/pequiv.dta", clear
merge 1:1 pid syear using "/Users/[AUTHOR]/Desktop/Housing Project/SOEP/soep_master_final_nomissingPID.dta"
drop if _merge==1
drop _merge
rename syear year
save "/Users/[AUTHOR]/Desktop/Housing Project/SOEP/soep_master_final_nomissingPID.dta", replace



* Inclusion of attitudinal variables
use pid syear plj0433 plj0434 using "/Users/[AUTHOR]/Desktop/Housing Project/SOEP/pl.dta", clear
rename syear year
merge 1:1 pid year using "/Users/[AUTHOR]/Desktop/Housing Project/SOEP/soep_master_final_nomissingPID.dta"



* Recoding of variables

* Attitudinal variables
recode plj0433 plj0434 (-8/-1 = .)

* HH net income
replace i11102 = . if i11102<0
gen i11102or = i11102  // original HH net income variable that has not been adjusted for HH size yet
replace i11102 = i11102/12  // converts annual income into monthly income
replace i11102 = i11102/sqrt(d11106)  // takes HH size into account (square root scale)
replace i11102 = 20000 if i11102>20000 & i11102<.
replace i11102 = i11102/1000
* HH net income measure unadjusted for HH size
gen i11102_unadjHH = i11102or
replace i11102_unadjHH = i11102_unadjHH/12  // converts annual income into monthly income
replace i11102_unadjHH = 20000 if i11102_unadjHH>20000 & i11102_unadjHH<.
replace i11102_unadjHH = i11102_unadjHH/1000

* HH income with alternative equivalence scales: "Old OECD scale" and "OECD-modified" scale
* d11106: HH size
* h11101: number of children 14 years old or younger
* "Old OECD scale"
gen weightold = 1.0 + 0.5*h11101 + 0.7*(d11106-h11101-1)  // 1 for 1st adult in HH, .5 for children below 14 years, .7 for other adults
gen i11102old = i11102or
replace i11102old = i11102old/12  // converts annual income into monthly income
replace i11102old = i11102old/weightold  // takes HH size into account
replace i11102old = 20000 if i11102old>20000 & i11102old<.
replace i11102old = i11102old/1000
* "OECD-modified" scale
gen weightnew = 1.0 + 0.3*h11101 + 0.5*(d11106-h11101-1)  // 1 for 1st adult in HH, .3 for children below 14 years, .5 for other adults
gen i11102new = i11102or
replace i11102new = i11102new/12  // converts annual income into monthly income
replace i11102new = i11102new/weightnew  // takes HH size into account
replace i11102new = 20000 if i11102new>20000 & i11102new<.
replace i11102new = i11102new/1000


* Personal net income
replace net_p_income = . if net_p_income<0
replace net_p_income = 10000 if net_p_income>10000 & net_p_income<.
replace net_p_income = net_p_income/1000

* Rent
replace monthly_rent = . if monthly_rent<0
gen monthly_rentor = monthly_rent  // original rent variable that has not been adjusted for HH size yet
* Square root scale
replace monthly_rent = monthly_rent/sqrt(d11106)  // takes HH size into account
replace monthly_rent = 10000 if monthly_rent>10000 & monthly_rent<.
replace monthly_rent = monthly_rent/1000
* Rent unadjusted for HH size
gen monthly_rent_unadjHH = monthly_rentor
replace monthly_rent_unadjHH = 10000 if monthly_rent_unadjHH>10000 & monthly_rent_unadjHH<.
replace monthly_rent_unadjHH = monthly_rent_unadjHH/1000
* "Old OECD scale"
gen monthly_rentold = monthly_rentor
replace monthly_rentold = monthly_rentold/weightold  // takes HH size into account
replace monthly_rentold = 10000 if monthly_rentold>10000 & monthly_rentold<.
replace monthly_rentold = monthly_rentold/1000
* "OECD-modified" scale
gen monthly_rentnew = monthly_rentor
replace monthly_rentnew = monthly_rentnew/weightnew  // takes HH size into account
replace monthly_rentnew = 10000 if monthly_rentnew>10000 & monthly_rentnew<.
replace monthly_rentnew = monthly_rentnew/1000

* Rent as share of income: Share rent/income
gen rentsh = monthly_rent/i11102
replace rentsh = . if rentsh>1 & rentsh!=.
gen rentshold = monthly_rentold/i11102old
replace rentshold = . if rentshold>1 & rentshold!=.
gen rentshnew = monthly_rentnew/i11102new
replace rentshnew = . if rentshnew>1 & rentshnew!=.
gen rentsh_unadjHH = monthly_rent_unadjHH/i11102_unadjHH
replace rentsh_unadjHH = . if rentsh_unadjHH>1 & rentsh_unadjHH!=.

* Party voted/vote choice - AfD
* see https://paneldata.org/soep-core/datasets/pl/plh0333
gen vcafd = .
replace vcafd = 0 if party_voted>0 & party_voted<27
replace vcafd = 1 if party_voted==27 | party_voted==30 | party_voted==31

* Party voted - Left
gen vclinke = .
replace vclinke = 0 if (party_voted>0 & party_voted<28) | party_voted==30 | party_voted==31
replace vclinke = 1 if party_voted==6 // Linke

gen vcspd = .
replace vcspd = 0 if (party_voted>0 & party_voted<28) | party_voted==30 | party_voted==31
replace vcspd = 1 if party_voted==1 // SPD

gen vccdu = .
replace vccdu = 0 if (party_voted>0 & party_voted<28) | party_voted==30 | party_voted==31
replace vccdu = 1 if party_voted==2 | party_voted==3 | party_voted==13 // CDU + CSU

gen vcfdp = .
replace vcfdp = 0 if (party_voted>0 & party_voted<28) | party_voted==30 | party_voted==31
replace vcfdp = 1 if party_voted==4 // FDP (Liberals)

gen vcgreen = .
replace vcgreen = 0 if (party_voted>0 & party_voted<28) | party_voted==30 | party_voted==31
replace vcgreen = 1 if party_voted==5 // Greens


* Identifying those who moved
gen mv=0 if hlf0107_h!=.
replace mv=1 if hlf0107_h>=2014 & hlf0107_h<=2016
replace mv=1 if hlf0107_h==2017 & (hlf0106>=1 & hlf0106<=8)
sort persnr
by persnr: egen mov = max(mv)

keep if year==2014 | year==2018
tab year, gen(z)
drop if persnr==.

xtset persnr year
xtset



************
* FIGURE 1 *
************

sort persnr year
by persnr: gen monthly_rentl = monthly_rent[_n-1]
gen monthly_rentd = monthly_rent - monthly_rentl
by persnr: gen rentshl = rentsh[_n-1]
gen rentshd = rentsh - rentshl
sort hid
by hid: egen hhsmax=max(d11106)
by hid: egen hhsmin=min(d11106)
gen hhsch = . // change in HH size
replace hhsch = 0 if hhsmax==hhsmin & hhsmax!=. & hhsmin!=.
replace hhsch = 1 if hhsmax>hhsmin & hhsmax!=. & hhsmin!=.

* The following (optional) graphic scheme requires the installation of the gr0070 package from Bischof (2017)
* Bischof, Daniel. 2017. "New Graphic Schemes for Stata: plotplain and plottig." The Stata Journal 17(3): 748-759.
* set scheme plotplain
hist monthly_rentd if mov==0 & vcafd!=. & rentshd!=. & lfs!=3 & (pop==1 | pop==2), percent ///
xtitle("Change in monthly rent (in Euros)") title("(a) Does not relocate") xscale(range(-1 1)) ///
xlabel(-1 "-1000" -0.5 "-500" 0 "0" 0.5 "500" 1 "1000", angle(horizontal)) ///
xtick(-1(0.5)1) yscale(range(0 40)) ylabel(0 "0" 10 "10" 20 "20" 30 "30" 40 "40", angle(horizontal)) ///
ytick(0(10)40)
graph save "fa.gph", replace
hist monthly_rentd if mov==1 & vcafd!=. & rentshd!=. & lfs!=3 & (pop==1 | pop==2), percent ///
xtitle("Change in monthly rent (in Euros)") title("(d) Relocates") xscale(range(-1 1)) ///
xlabel(-1 "-1000" -0.5 "-500" 0 "0" 0.5 "500" 1 "1000", angle(horizontal)) ///
xtick(-1(0.5)1) yscale(range(0 40)) ylabel(0 "0" 10 "10" 20 "20" 30 "30" 40 "40", angle(horizontal)) ///
ytick(0(10)40)
graph save "fb.gph", replace
hist monthly_rentd if hhsch==0 & vcafd!=. & rentshd!=. & lfs!=3 & (pop==1 | pop==2), percent ///
xtitle("Change in monthly rent (in Euros)") title("(b) HH size does not change") xscale(range(-1 1)) ///
xlabel(-1 "-1000" -0.5 "-500" 0 "0" 0.5 "500" 1 "1000", angle(horizontal)) ///
xtick(-1(0.5)1) yscale(range(0 40)) ylabel(0 "0" 10 "10" 20 "20" 30 "30" 40 "40", angle(horizontal)) ///
ytick(0(10)40)
graph save "fc.gph", replace
hist monthly_rentd if hhsch==1 & vcafd!=. & rentshd!=. & lfs!=3 & (pop==1 | pop==2), percent ///
xtitle("Change in monthly rent (in Euros)") title("(e) HH size changes") xscale(range(-1 1)) ///
xlabel(-1 "-1000" -0.5 "-500" 0 "0" 0.5 "500" 1 "1000", angle(horizontal)) ///
xtick(-1(0.5)1) yscale(range(0 40)) ylabel(0 "0" 10 "10" 20 "20" 30 "30" 40 "40", angle(horizontal)) ///
ytick(0(10)40)
graph save "fd.gph", replace
hist monthly_rentd if hhsch==0 & mov==0 & vcafd!=. & rentshd!=. & lfs!=3 & (pop==1 | pop==2), percent ///
xtitle("Change in monthly rent (in Euros)") title("(c) Neither relocates nor HH size changes") xscale(range(-1 1)) ///
xlabel(-1 "-1000" -0.5 "-500" 0 "0" 0.5 "500" 1 "1000", angle(horizontal)) ///
xtick(-1(0.5)1) yscale(range(0 40)) ylabel(0 "0" 10 "10" 20 "20" 30 "30" 40 "40", angle(horizontal)) ///
ytick(0(10)40)
graph save "fe.gph", replace
hist monthly_rentd if hhsch==1 & mov==1 & vcafd!=. & rentshd!=. & lfs!=3 & (pop==1 | pop==2), percent ///
xtitle("Change in monthly rent (in Euros)") title("(f) Relocates and HH size changes") xscale(range(-1 1)) ///
xlabel(-1 "-1000" -0.5 "-500" 0 "0" 0.5 "500" 1 "1000", angle(horizontal)) ///
xtick(-1(0.5)1) yscale(range(0 40)) ylabel(0 "0" 10 "10" 20 "20" 30 "30" 40 "40", angle(horizontal)) ///
ytick(0(10)40)
graph save "ff.gph", replace
graph combine fa.gph fc.gph fe.gph fb.gph fd.gph ff.gph
graph export "f0.pdf", as(pdf) replace




**********************
* RESULTS IN TABLE 1 *
**********************

* TABLE 1, columns 1-3:
xtreg vcafd monthly_rent c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2), fe vce(cluster persnr)
xtreg vcafd c.monthly_rent##c.monthly_rent c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2), fe vce(cluster persnr)
xtreg vcafd c.rentsh##c.rentsh c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2), fe vce(cluster persnr)

* TABLE 1, columns 4:
* The replication of results in column 4 in Table 1 requires access to more restricted regional
* data, which is not included in SOEP-Core v36i (International Edition). See code in line 1644
* below to replicate the results in column 4 in Table 1 with more restricted regional data.

* TABLE 1, column 5:
gen rural=0
replace rural=1 if regtyp==2
xtreg vcafd c.monthly_rent##c.monthly_rent c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2) & rural==0, fe vce(cluster persnr)

* TABLE 1, columns 6 and 7:
sort year
by year: egen p33 = pctile(i11102) if  vcafd!=. & monthly_rent!=. & lfs!=3 & (pop==1 | pop==2), p(33.3)
tab p33 year
gen pr=.
replace pr=0 if monthly_rent!=.
replace pr=1 if i11102<1.366219 & year==2014
replace pr=1 if i11102<1.443866 & year==2018
sort persnr
by persnr: egen incbt=max(pr)
xtreg vcafd c.monthly_rent##c.monthly_rent c.i11102##c.i11102 z* if incbt==1 & lfs!=3 & (pop==1 | pop==2), fe vce(cluster persnr)
xtreg vcafd c.monthly_rent##c.monthly_rent c.i11102##c.i11102 z* if incbt==0 & lfs!=3 & (pop==1 | pop==2), fe vce(cluster persnr)

* FIGURE 2
replace monthly_rent=monthly_rent*1000
xtreg vcafd c.monthly_rent##c.monthly_rent c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2), fe vce(cluster persnr)
margins, at(monthly_rent=(-1000 1000) monthly_rent=(-1000(200)1000))
marginsplot, title("") xtitle("Change in monthly rent (in Euros)")   ///
	ytitle("Probability of Voting AfD")
graph export "f1.pdf", as(pdf) replace
replace monthly_rent=monthly_rent/1000




**********************
* RESULTS IN TABLE 2 *
**********************

sort persnr year
by persnr: gen vcafdl = vcafd[_n-1]
by persnr: gen i11102l = i11102[_n-1]
gen i11102d = i11102 - i11102l
recode sex (-1=.)(1=0)(2=1), gen(female)
replace gebjahr=. if gebjahr==-1
gen age=-(gebjahr-2020)
gen married=0
replace married=1 if d11104==1
gen educ=0 // High school
replace educ=1 if d11108==2
gen educ2=0 // More than high school
replace educ2=1 if d11108==3
* plj0433: refugees good for economy
* plj0434: refugees enrich cultural life

* TABLE 2, columns 1-3:
reg plj0433 monthly_rentd monthly_rent i11102d i11102 female age married educ educ2 rural if year==2018 & lfs!=3 & (pop==1 | pop==2), robust
gen incnbt=-(incbt-1)  // those not in bottom income third in 2014 or 2018
reg plj0433 c.monthly_rentd##i.incnbt monthly_rent i11102d i11102 female age married educ educ2 rural if year==2018 & lfs!=3 & (pop==1 | pop==2), robust
reg plj0433 c.monthly_rentd##c.i11102 monthly_rent i11102d female age married educ educ2 rural if year==2018 & lfs!=3 & (pop==1 | pop==2), robust

* TABLE 2, columns 4-6:
reg plj0434 monthly_rentd monthly_rent i11102d i11102 female age married educ educ2 rural if year==2018 & lfs!=3 & (pop==1 | pop==2), robust
reg plj0434 c.monthly_rentd##i.incnbt monthly_rent i11102d i11102 female age married educ educ2 rural if year==2018 & lfs!=3 & (pop==1 | pop==2), robust
reg plj0434 c.monthly_rentd##c.i11102 monthly_rent i11102d female age married educ educ2 rural if year==2018 & lfs!=3 & (pop==1 | pop==2), robust

* TABLE 2, columns 7-9:
reg vcafd monthly_rentd monthly_rent i11102d i11102 female age married educ educ2 rural vcafdl if year==2018 & lfs!=3 & (pop==1 | pop==2), robust
reg vcafd monthly_rentd monthly_rent i11102d i11102 plj0433 plj0434 female age married educ educ2 rural vcafdl if year==2018 & lfs!=3 & (pop==1 | pop==2), robust
reg vcafd c.monthly_rentd##c.i11102 monthly_rent i11102d plj0433 plj0434 female age married educ educ2 rural vcafdl if year==2018 & lfs!=3 & (pop==1 | pop==2), robust

* FIGURE 3
replace monthly_rentd=monthly_rentd*1000
replace i11102=i11102*1000
rename i11102 Income
reg plj0433 c.monthly_rentd##c.Income monthly_rent i11102d female age married educ educ2 rural if year==2018 & lfs!=3 & (pop==1 | pop==2), robust
margins, at(monthly_rentd=(-1000(250)1000) Income=(1000 4000))
marginsplot, title("") xtitle("Change in monthly rent (in Euros)")   ///
	ytitle("Refugees good for economy") legend(rows(1) position(6))
graph save "f2.gph", replace

reg plj0434 c.monthly_rentd##c.Income monthly_rent i11102d female age married educ educ2 rural if year==2018 & lfs!=3 & (pop==1 | pop==2), robust
margins, at(monthly_rentd=(-1000(250)1000) Income=(1000 4000))
marginsplot, title("") xtitle("Change in monthly rent (in Euros)")    ///
	ytitle("Refugees enrich cultural life") legend(rows(1) position(6))
graph save "f3.gph", replace
graph combine f2.gph f3.gph
graph export "f2.pdf", as(pdf) replace
replace monthly_rentd=monthly_rentd/1000
rename Income i11102
replace i11102=i11102/1000



************************************
* RESULTS IN SUPPLEMENTAL MATERIAL *
************************************

* TABLE 2: Descriptive statistics 1
sum monthly_rentd if mov==0 & vcafd!=. & rentshd!=. & lfs!=3 & (pop==1 | pop==2)
sum monthly_rentd if mov==1 & vcafd!=. & rentshd!=. & lfs!=3 & (pop==1 | pop==2)
sum monthly_rentd if hhsch==0 & vcafd!=. & rentshd!=. & lfs!=3 & (pop==1 | pop==2)
sum monthly_rentd if hhsch==1 & vcafd!=. & rentshd!=. & lfs!=3 & (pop==1 | pop==2)
sum monthly_rentd if hhsch==0 & mov==0 & vcafd!=. & rentshd!=. & lfs!=3 & (pop==1 | pop==2)
sum monthly_rentd if hhsch==1 & mov==1 & vcafd!=. & rentshd!=. & lfs!=3 & (pop==1 | pop==2)


* TABLE 3: Descriptive statistics 2
sum monthly_rentd monthly_rent i11102d i11102 plj0433 female age married educ educ2 rural if year==2018 & lfs!=3 & (pop==1 | pop==2) & monthly_rentd!=. & monthly_rent!=. & i11102d!=. & i11102!=. & female!=. & age!=. & married!=. & educ!=. & educ2!=. & rural!=. & plj0433!=.
sum plj0434 if year==2018 & lfs!=3 & (pop==1 | pop==2) & monthly_rentd!=. & monthly_rent!=. & i11102d!=. & i11102!=. & female!=. & age!=. & married!=. & educ!=. & educ2!=. & rural!=.
sum vcafdl if year==2018 & lfs!=3 & (pop==1 | pop==2) & monthly_rentd!=. & monthly_rent!=. & i11102d!=. & i11102!=. & female!=. & age!=. & married!=. & educ!=. & educ2!=. & rural!=.


* TABLE 4: Fixed-effects logistic regression
xtlogit vcafd c.monthly_rent##c.monthly_rent c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2), fe


* TABLE 5: Fixed-effects regression results for those who relocate and those who do not relocate
xtreg vcafd c.monthly_rent##c.monthly_rent##i.mov c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2), fe vce(cluster persnr)
xtlogit vcafd c.monthly_rent##c.monthly_rent##i.mov c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2), fe


* FIGURE 1
replace monthly_rent=monthly_rent*1000
xtreg vcafd c.monthly_rent##c.monthly_rent c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2) & mov==0, fe vce(cluster persnr)
margins, at(monthly_rent=(-1000 1000) monthly_rent=(-1000(200)1000))
marginsplot, title("Does not relocate") xtitle("Change in monthly rent (in Euros)")   ///
	ytitle("Probability of Voting AfD")
graph save "f4.gph", replace
xtreg vcafd c.monthly_rent##c.monthly_rent c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2) & mov==1, fe vce(cluster persnr)
margins, at(monthly_rent=(-1000 1000) monthly_rent=(-1000(200)1000))
marginsplot, title("Relocates") xtitle("Change in monthly rent (in Euros)")   ///
	ytitle("Probability of Voting AfD")
graph save "f5.gph", replace
graph combine f4.gph f5.gph
graph export "f3.pdf", as(pdf) replace
replace monthly_rent=monthly_rent/1000


* TABLE 6: Results with controls for number and age of HH members
* d11101: age
* d11106: household size
replace d11101 = . if d11101<0
sort hid year
by hid year: egen hhage = mean(d11101) // average age of HH members
xtreg vcafd monthly_rent c.i11102##c.i11102 d11106 hhage z* if lfs!=3 & (pop==1 | pop==2), fe vce(cluster persnr)
xtreg vcafd c.monthly_rent##c.monthly_rent c.i11102##c.i11102 d11106 hhage z* if lfs!=3 & (pop==1 | pop==2), fe vce(cluster persnr)
xtreg vcafd c.rentsh##c.rentsh c.i11102##c.i11102 d11106 hhage z* if lfs!=3 & (pop==1 | pop==2), fe vce(cluster persnr)
* Column 4: See code in line 1648 below.
xtreg vcafd c.monthly_rent##c.monthly_rent c.i11102##c.i11102 d11106 hhage z* if lfs!=3 & (pop==1 | pop==2) & rural==0, fe vce(cluster persnr)
xtreg vcafd c.monthly_rent##c.monthly_rent c.i11102##c.i11102 d11106 hhage z* if incbt==1 & lfs!=3 & (pop==1 | pop==2), fe vce(cluster persnr)
xtreg vcafd c.monthly_rent##c.monthly_rent c.i11102##c.i11102 d11106 hhage z* if incbt==0 & lfs!=3 & (pop==1 | pop==2), fe vce(cluster persnr)


* TABLE 7: "Old OECD scale"
xtreg vcafd monthly_rentold c.i11102old##c.i11102old z* if lfs!=3 & (pop==1 | pop==2), fe vce(cluster persnr)
xtreg vcafd c.monthly_rentold##c.monthly_rentold c.i11102old##c.i11102old z* if lfs!=3 & (pop==1 | pop==2), fe vce(cluster persnr)
xtreg vcafd c.rentshold##c.rentshold c.i11102old##c.i11102old z* if lfs!=3 & (pop==1 | pop==2), fe vce(cluster persnr)
* Column 4: See code in line 1651 below.
xtreg vcafd c.monthly_rentold##c.monthly_rentold c.i11102old##c.i11102old z* if lfs!=3 & (pop==1 | pop==2) & rural==0, fe vce(cluster persnr)
xtreg vcafd c.monthly_rentold##c.monthly_rentold c.i11102old##c.i11102old z* if incbt==1 & lfs!=3 & (pop==1 | pop==2), fe vce(cluster persnr)
xtreg vcafd c.monthly_rentold##c.monthly_rentold c.i11102old##c.i11102old z* if incbt==0 & lfs!=3 & (pop==1 | pop==2), fe vce(cluster persnr)


* TABLE 8: "OECD-modified" scale
xtreg vcafd monthly_rentnew c.i11102new##c.i11102new z* if lfs!=3 & (pop==1 | pop==2), fe vce(cluster persnr)
xtreg vcafd c.monthly_rentnew##c.monthly_rentnew c.i11102new##c.i11102new z* if lfs!=3 & (pop==1 | pop==2), fe vce(cluster persnr)
xtreg vcafd c.rentshnew##c.rentshnew c.i11102new##c.i11102new z* if lfs!=3 & (pop==1 | pop==2), fe vce(cluster persnr)
* Column 4: See code in line 1654 below.
xtreg vcafd c.monthly_rentnew##c.monthly_rentnew c.i11102new##c.i11102new z* if lfs!=3 & (pop==1 | pop==2) & rural==0, fe vce(cluster persnr)
xtreg vcafd c.monthly_rentnew##c.monthly_rentnew c.i11102new##c.i11102new z* if incbt==1 & lfs!=3 & (pop==1 | pop==2), fe vce(cluster persnr)
xtreg vcafd c.monthly_rentnew##c.monthly_rentnew c.i11102new##c.i11102new z* if incbt==0 & lfs!=3 & (pop==1 | pop==2), fe vce(cluster persnr)


* TABLE 9: Results with rent and income measures that are unadjusted for HH size
xtreg vcafd monthly_rent_unadjHH c.i11102_unadjHH##c.i11102_unadjHH z* if lfs!=3 & (pop==1 | pop==2), fe vce(cluster persnr)
xtreg vcafd c.monthly_rent_unadjHH##c.monthly_rent_unadjHH c.i11102_unadjHH##c.i11102_unadjHH z* if lfs!=3 & (pop==1 | pop==2), fe vce(cluster persnr)
xtreg vcafd c.rentsh_unadjHH##c.rentsh_unadjHH c.i11102_unadjHH##c.i11102_unadjHH z* if lfs!=3 & (pop==1 | pop==2), fe vce(cluster persnr)
* Column 4: See code in line 1657 below.
xtreg vcafd c.monthly_rent_unadjHH##c.monthly_rent_unadjHH c.i11102_unadjHH##c.i11102_unadjHH z* if lfs!=3 & (pop==1 | pop==2) & rural==0, fe vce(cluster persnr)
xtreg vcafd c.monthly_rent_unadjHH##c.monthly_rent_unadjHH c.i11102_unadjHH##c.i11102_unadjHH z* if incbt==1 & lfs!=3 & (pop==1 | pop==2), fe vce(cluster persnr)
xtreg vcafd c.monthly_rent_unadjHH##c.monthly_rent_unadjHH c.i11102_unadjHH##c.i11102_unadjHH z* if incbt==0 & lfs!=3 & (pop==1 | pop==2), fe vce(cluster persnr)


* TABLE 10: Heterogeneities by geographic area
* The replication of results in Table 10 requires access to more restricted regional
* data, which is not included in SOEP-Core v36i (International Edition). See below for code to
* replicate the results in Table 10 with more restricted regional data.


* TABLE 11: Heterogeneities by generation
replace gebjahr=. if gebjahr<0
xtreg vcafd monthly_rent c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2) & gebjahr>=1981 & gebjahr<=1996, fe vce(cluster persnr)
xtreg vcafd c.monthly_rent##c.monthly_rent c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2) & gebjahr>=1981 & gebjahr<=1996, fe vce(cluster persnr)
xtreg vcafd monthly_rent c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2) & gebjahr>=1965 & gebjahr<=1980, fe vce(cluster persnr)
xtreg vcafd c.monthly_rent##c.monthly_rent c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2) & gebjahr>=1965 & gebjahr<=1980, fe vce(cluster persnr)
xtreg vcafd monthly_rent c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2) & gebjahr>=1946 & gebjahr<=1964, fe vce(cluster persnr)
xtreg vcafd c.monthly_rent##c.monthly_rent c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2) & gebjahr>=1946 & gebjahr<=1964, fe vce(cluster persnr)
xtreg vcafd monthly_rent c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2) & gebjahr>=1928 & gebjahr<=1945, fe vce(cluster persnr)
xtreg vcafd c.monthly_rent##c.monthly_rent c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2) & gebjahr>=1928 & gebjahr<=1945, fe vce(cluster persnr)


* TABLE 12: Results for other parties
xtreg vclinke monthly_rent c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2), fe vce(cluster persnr)
xtreg vclinke c.monthly_rent##c.monthly_rent c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2), fe vce(cluster persnr)
xtlogit vclinke monthly_rent c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2), fe
xtreg vcspd c.monthly_rent##c.monthly_rent c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2), fe vce(cluster persnr)
xtreg vcgreen c.monthly_rent##c.monthly_rent c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2), fe vce(cluster persnr)
xtreg vcfdp c.monthly_rent##c.monthly_rent c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2), fe vce(cluster persnr)
xtreg vccdu c.monthly_rent##c.monthly_rent c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2), fe vce(cluster persnr)


* TABLE 13: Results based on prior vote choice for other parties
gen vclinkep = vclinke if year==2014
sort pid
by pid: egen vclinkepr = max(vclinkep)
xtreg vcafd c.monthly_rent##c.monthly_rent c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2) & vclinkepr==1, fe vce(cluster persnr)
gen vcspdp = vcspd if year==2014
by pid: egen vcspdpr = max(vcspdp)
xtreg vcafd c.monthly_rent##c.monthly_rent c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2) & vcspdpr==1, fe vce(cluster persnr)
gen vcgreenp = vcgreen if year==2014
by pid: egen vcgreenpr = max(vcgreenp)
xtreg vcafd c.monthly_rent##c.monthly_rent c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2) & vcgreenpr==1, fe vce(cluster persnr)
gen vcfdpp = vcfdp if year==2014
by pid: egen vcfdppr = max(vcfdpp)
xtreg vcafd c.monthly_rent##c.monthly_rent c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2) & vcfdppr==1, fe vce(cluster persnr)
gen vccdup = vccdu if year==2014
by pid: egen vccdupr = max(vccdup)
xtreg vcafd c.monthly_rent##c.monthly_rent c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2) & vccdupr==1, fe vce(cluster persnr)


* TABLE 14: Results with controls for government-subsidized housing and government housing assistance
gen soch = . // respondent lives in social/government subsidized housing
replace soch=0 if hlf0011_h==2
replace soch=1 if hlf0011_h==1
replace hlc0084_h = 0 if hlc0084_h==-2 // amount of housing assistance from government (in euros)
replace hlc0084_h = . if hlc0084_h<0
replace hlc0084_h = hlc0084_h/1000
xtreg vcafd c.monthly_rent##c.monthly_rent soch c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2), fe vce(cluster persnr)
xtreg vcafd c.monthly_rent##c.monthly_rent hlc0084_h c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2), fe vce(cluster persnr)


* TABLE 15: Logistic regression of 2017 AfD vote choice
logit vcafd monthly_rentd monthly_rent i11102d i11102 female age married educ educ2 rural vcafdl if year==2018 & lfs!=3 & (pop==1 | pop==2), robust
logit vcafd monthly_rentd monthly_rent i11102d i11102 plj0433 plj0434 female age married educ educ2 rural vcafdl if year==2018 & lfs!=3 & (pop==1 | pop==2), robust





* HETEROGENEITIES BY GEOGRAPHIC AREA
* The replication of the results in column 4 in Table 1 in the main article and
* in column 4 in Tables 6, 7, 8 and 9 in the Appendix and of results in Table 10
* in the Appendix requires access to more restricted regional data that is included
* in SOEP-Core v36eu (EU edition; https://doi.org/10.5684/soep.core.v36eu).
rename year syear
drop _merge
merge m:1 hid syear using "/Users/[AUTHOR]/Desktop/Housing Project/SOEP/regionl.dta"
drop if _merge==2
rename syear year
gen east=0 if nuts1>=1 & nuts1<=16 & nuts1!=3
replace east=1 if nuts1==4 | nuts1==8 | nuts1==13 | nuts1==14 | nuts1==16

* MAIN ARTICLE
* TABLE 1, column 4:
xtreg vcafd c.monthly_rent##c.monthly_rent c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2) & east==0, fe vce(cluster persnr)

* APPENDIX
* TABLE 6, column 4:
xtreg vcafd c.monthly_rent##c.monthly_rent c.i11102##c.i11102 d11106 hhage z* if lfs!=3 & (pop==1 | pop==2) & east==0, fe vce(cluster persnr)

* TABLE 7, column 4:
xtreg vcafd c.monthly_rentold##c.monthly_rentold c.i11102old##c.i11102old z* if lfs!=3 & (pop==1 | pop==2) & east==0, fe vce(cluster persnr)

* TABLE 8, column 4:
xtreg vcafd c.monthly_rentnew##c.monthly_rentnew c.i11102new##c.i11102new z* if lfs!=3 & (pop==1 | pop==2) & east==0, fe vce(cluster persnr)

* TABLE 9, column 4:
xtreg vcafd c.monthly_rent_unadjHH##c.monthly_rent_unadjHH c.i11102_unadjHH##c.i11102_unadjHH z* if lfs!=3 & (pop==1 | pop==2) & east==0, fe vce(cluster persnr)

* TABLE 10
xtreg vcafd c.monthly_rent##c.monthly_rent c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2) & (east==0 | nuts1==3), fe vce(cluster persnr)
xtreg vcafd c.monthly_rent##c.monthly_rent c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2) & east==0, fe vce(cluster persnr)
xtreg vcafd c.monthly_rent##c.monthly_rent c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2) & (east==1 | nuts1==3), fe vce(cluster persnr)
xtreg vcafd c.monthly_rent##c.monthly_rent c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2) & east==1, fe vce(cluster persnr)
xtreg vcafd c.monthly_rent##c.monthly_rent c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2) & nuts1==2, fe vce(cluster persnr)
xtreg vcafd c.monthly_rent##c.monthly_rent c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2) & nuts1==3, fe vce(cluster persnr)
xtreg vcafd c.monthly_rent##c.monthly_rent c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2) & rural==1, fe vce(cluster persnr)
xtreg vcafd c.monthly_rent##c.monthly_rent c.i11102##c.i11102 z* if lfs!=3 & (pop==1 | pop==2) & rural==0, fe vce(cluster persnr)

